package visual;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import main.GameDef;

public class Frame extends JFrame implements ActionListener, GameDef{
/**
	 * 
	 */
	private static final long serialVersionUID = -3622765970914975581L;
	/*
 * Atributes
 */
	Board boardCenter;
	JTextArea playerInfoTextBox;
	JTextArea gameInfoTextBox;
	JTextArea playerNotesTextBox;
	JButton buyButton;
	JButton attackButton;
	JButton repairButton;
	JButton finalizeButton;
/*
 * Constructors
 */
	public Frame(String windowName) throws HeadlessException {
		super(windowName);
		FrameHandler.addFrame(this);
		getContentPane().setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createMenu();
		setSize(800, 600);
		setLocationRelativeTo(null);
	}

/*
 * Methods
 */
	public void createAndShowGUI() {
        setVisible(true);
        setResizable(false);
	}
	
	private void createMenu(){
		JMenuBar menuBar;
		JMenu gameMenu;
		JMenuItem menuItem;
		menuBar = new JMenuBar();
		gameMenu = new JMenu(MENU_STRINGS[0]);
		gameMenu.setMnemonic(KeyEvent.VK_J);
		for(int i = 0; i < MENU_GAME_STRINGS.length; i++){
			menuItem = new JMenuItem(MENU_GAME_STRINGS[i].toString());
			if(i == MENU_GAME_STRINGS.length - 1){
				gameMenu.addSeparator();
			}
			menuItem.addActionListener(this);
			gameMenu.add(menuItem);

		}
		menuBar.add(gameMenu);
		setJMenuBar(menuBar);
	}

	public void actionPerformed(ActionEvent event) {
		FrameHandler.handleActionEvent(event, null);
		firePropertyChange(null, null, null);
	}
	
	public void close(){
		setVisible(false);
		dispose();
	}
	
	public void createGameBoard(){
		JPanel board = new JPanel(new BorderLayout());
		JPanel boardTop = new JPanel(new FlowLayout());
		JPanel boardLeft = new JPanel();
		boardLeft.setLayout(new BoxLayout(boardLeft, BoxLayout.PAGE_AXIS));
		JPanel boardRight = new JPanel(new FlowLayout());
		boardCenter = new Board();
		//panel top begin
		buyButton = new JButton(FRAME_BUTTON_STRINGS[FRAME_BUTTON_BUY]);
		buyButton.addActionListener(this);
		attackButton = new JButton(FRAME_BUTTON_STRINGS[FRAME_BUTTON_ATTACK]);
		attackButton.addActionListener(this);
		repairButton = new JButton(FRAME_BUTTON_STRINGS[FRAME_BUTTON_REPAIR]);
		repairButton.addActionListener(this);
		finalizeButton = new JButton(FRAME_BUTTON_STRINGS[FRAME_BUTTON_ENDTURN]);
		finalizeButton.addActionListener(this);
		boardTop.add(buyButton);
		boardTop.add(attackButton);
		boardTop.add(repairButton);
		boardTop.add(finalizeButton);
		boardTop.setBorder(BorderFactory.createTitledBorder("Acciones"));
		//panel top end
		//panel left begin
		gameInfoTextBox = new JTextArea(14, 12);
		gameInfoTextBox.setEditable(false);
		JScrollPane leftTopScrollPanel = new JScrollPane(gameInfoTextBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		leftTopScrollPanel.setBorder(BorderFactory.createTitledBorder("Inf. del server"));
		playerNotesTextBox = new JTextArea(14, 12);
		playerNotesTextBox.setEditable(true);
		JScrollPane leftBottomScrollPanel = new JScrollPane(playerNotesTextBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		leftBottomScrollPanel.setBorder(BorderFactory.createTitledBorder("Notas"));
		boardLeft.add(leftTopScrollPanel);
		boardLeft.add(leftBottomScrollPanel);
		//panel left end
		//panel right begin
		playerInfoTextBox = new JTextArea(0, 13);
		playerInfoTextBox.setEditable(false);
		playerInfoTextBox.setBorder(BorderFactory.createTitledBorder("Inf. del jugador"));
		boardRight.add(playerInfoTextBox);
		//panel right end
		board.add(boardTop, BorderLayout.NORTH);
		board.add(boardLeft, BorderLayout.WEST);
		board.add(boardRight, BorderLayout.EAST);
		board.add(boardCenter, BorderLayout.CENTER);
		setContentPane(board);
		validate();
	}
}
